<?php
class subject extends Model
{
	/**
	 * 添加科目
	 * @param unknown_type $subjectName
	 * @param unknown_type $userID
	 * @return Ambigous <boolean, number>
	 */
	public function addSubject($subjectName,$userID)
	{
		$sql = "INSERT INTO `howdo`.`subject` 
				(`sub_id`, `sub_cont`, `sub_create_time`, `sub_create_admin_id`) 
				VALUES 
				(NULL, '".$subjectName."', NOW(), '".$userID."')";
		//echo $sql;
		return $this->insert($sql);
	}
	
	/**
	 * 根据科目ID修改科目名称
	 * @param unknown_type $subjectName
	 * @param unknown_type $subjectID
	 * @param unknown_type $userID
	 * @return resource
	 */
	public function editSubject($subjectName, $subjectID, $userID)
	{
		$sql = "UPDATE `howdo`.`subject` 
				SET 
				`sub_cont` = '".$subjectName."', 
				`sub_create_time` = NOW(), 
				`sub_create_admin_id` = '".$userID."' 
				WHERE `subject`.`sub_id` = '".$subjectID."'";
		return $this->update($sql);
	}
	
	/**
	 * 根据科目名称获取科目
	 * @param unknown_type $name
	 */
	public function getSubjectByName($name){
		$sql = "SELECT `subject`.* FROM `subject` WHERE `sub_cont` = '".$name."'"; 
		return $this->fetchRow($sql);
		
	}
	/**
	 * 根据ID获取科目详情
	 * @param unknown_type $subjectID
	 * @return Ambigous <boolean, multitype:>
	 */
	 public function getSubjectInfoByID($subjectID)
	 {
	 	$sql = "SELECT `subject`.*,`admin`.* FROM `subject`
	 			LEFT JOIN `admin` ON `subject`.`sub_create_admin_id` = `admin`.`admin_id`
	 		    WHERE `sub_id` = '".$subjectID."'"; 
	 	return $this->fetchRow($sql);
	 }
	 
	 /**
	  * 获取科目列表以及创建者信息
	  * @return Ambigous <boolean, multitype:>
	  */
	 public function getSubjectList()
	 {
	 	$sql = "SELECT `subject`.*,`admin`.`admin_realname` FROM `subject`
	 			LEFT JOIN `admin` ON `subject`.`sub_create_admin_id` = `admin`.`admin_id`
	 			ORDER BY sub_create_time DESC;";
	 	return $this->fetchAll($sql);
	 }
	 
	 /**
	  * 删除科目
	  * @param unknown_type $subjectID
	  * @return resource
	  */
	 public function delSubject($subjectID)
	 {
	 	$num = $this->getExerciseTypeNumFromEt($subjectID);
	 	if($num>0){
	 		return -1;
	 	}
	 	else{
	 		$sql = "DELETE FROM `howdo`.`subject` WHERE `subject`.`sub_id` = '".$subjectID."'";
	 		return $this->del($sql) ? 1 : 0;
	 	}
	 }
	 /**
	  * 获取所有科目
	  */
	 public function getSubjects()
	 {
	 	$sql = "SELECT `sub_id`,`sub_cont` FROM `subject`";
	 	return $this->fetchAll($sql);
	 }
	 	 
	 /**
	  * 获取科目下题目类型数目
	  * @param unknown_type $subid
	  * @return number
	  */
	 public function getExerciseTypeNumFromEt($subid){
	 	return $this->getTotal("exercise_type","`sub_id`=".$subid);
	 }
}